<?php
namespace app\api\controller;
use app\api\ApiController;
use common\service\AuthService;
use common\service\InvService;

/**
 * 
 * @author Administrator
 *
 */
class Userinv extends ApiController
{
	public $steamInfo = [];//用户steam信息

	public function __construct()
	{
		parent::__construct();
		$this->steamInfo = AuthService::getInstance()->verifyToken();
		$this->steamInfo = empty($this->steamInfo) ? [] : $this->steamInfo;
	}
	
	public function refreshItem($appId=730){
		if(empty($this->steamInfo)){
			return $this->errorMessage(BAD_ACCESS_TOKEN_NO, BAD_ACCESS_TOKEN_ERROR);
		}
		$steamId=$this->steamInfo['steam_id'];
		$list = InvService::getInstance()->refreshItem($steamId,$appId);
		return $this->successJson();
	}
	
 	public function lists($appId=730,$page=1,$limit=25,$sort='desc',$keyword='',$order='price'){
        $result = $this->validate(
            ['appId' => $appId,
                'page'  => $page,
                'limit' => $limit,
                'order' => $order,
                'sort'  => $sort,
                'keyword' => $keyword],
            ['appId' => 'number',
                'page'  => 'number',
                'limit' => 'number',
                'sort'  => 'alpha',]
        );
 		if(true !== $result){
 			// 验证失败 输出错误信息
 			return $this->errorParams($result);
 		}
 		if(empty($this->steamInfo)){
 			return $this->errorMessage(BAD_ACCESS_TOKEN_NO, BAD_ACCESS_TOKEN_ERROR);
 		}
 		$steamId=$this->steamInfo['steam_id'];
 		$userId = $this->steamInfo['user_id'];
 		
 		$list = InvService::getInstance()->getInv($userId,$steamId,$page,$limit,$appId,$sort,$keyword,$order);
		if($list === -1){
			return $this->errorMessage(BAD_APPID_NO, BAD_APPID_NO_ERROR);
		}else if ($list === -2) {
			return $this->errorMessage(USER_INVENTORY_NO_OPEN, USER_INVENTORY_NO_OPEN_ERROR);
		}else if ($list === -3) {
            return $this->errorMessage(USER_INVENTORY_NO, USER_INVENTORY_ERROR);
        }
		$result = InvService::getInstance()->getSelectedItemPrice($steamId,$userId,$appId);
		$result['list'] = $list['list'];
		$result['totalPage'] = $list['totalPage'];
		$result['currentPage'] = $list['currentPage'];
		$result['games'] = InvService::getInstance()->getAppIds($appId);
        $result['order'] = $order;
        $result['sort'] = $sort;
        $result['keyword'] = $keyword;
		return $this->successJson($result);
 	}
 	
 	public function selectItem($positionId,$appId=730){
 		$user = AuthService::getInstance()->verifyToken();
 		if(empty($user)){
 			return $this->errorMessage(BAD_ACCESS_TOKEN_NO, BAD_ACCESS_TOKEN_ERROR);
 		}
 		$steamId = $user['steam_id'];
 		$userId = $user['user_id'];
 		$item = InvService::getInstance()->selectItem($userId,$positionId,$steamId,$appId);
 		if($item == -1){
 			//请刷新库存 
 			return $this->errorMessage(STEAM_INVENTORY_CHANGE_NO,STEAM_INVENTORY_CHANGE_ERROR);
 		}
 		if($item == -2){
 			//当前物品不收 请重新刷新
 			return $this->errorMessage(STEAM_INVENTORY_CHANGE_NO,'当前物品不收 请重新刷新');
 		}
 		$result = InvService::getInstance()->getSelectedItemPrice($steamId,$userId,$appId);
 		$result['item'] = $item;
 		
 		return $this->successJson($result);
 	}
	
 	public function selectAllItem($status = 0,$appId=730){
 		if(empty($this->steamInfo)){
 			return $this->errorMessage(BAD_ACCESS_TOKEN_NO, BAD_ACCESS_TOKEN_ERROR);
 		}
 		$steamId=$this->steamInfo['steam_id'];
 		$userId = $this->steamInfo['user_id'];
 		InvService::getInstance()->selectAllItem($steamId,$userId,$status,$appId);
 		
 		return $this->successJson();
 	}
}
